<template>
  <el-dialog
    :title="title"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    class="dialog-form find-password"
    width="400px"
    @close="resetDialog"
    append-to-body
  >
    <el-form label-width="110px" size="small" :show-message="false">
      <el-row>
        <el-col :span="24">
        <el-form-item label="旧密码：" required>
          <el-input v-model.trim="form.old_password" placeholder="请输入" show-password />
        </el-form-item>
        </el-col>
        <el-col :span="24">
        <el-form-item label="新密码：" required>
          <el-input v-model="form.new_password" placeholder="请输入" show-password />
        </el-form-item>
        </el-col>
        <el-col :span="24">
        <el-form-item label="确认新密码：" required>
          <el-input v-model="new_repassword" placeholder="请输入" show-password />
        </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="isShow = false">取 消</el-button>
      <el-button :loading="btnLoading" size="small" type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data(){
    return{
      title: '修改密码',
      isShow: false,
      btnLoading: false,
      form:{
        old_password: null,
        new_password: null,
      },
      new_repassword: null,
      defaultForm: {},
    }
  },
  created(){
    this.defaultForm = JSON.parse(JSON.stringify(this.form));
  },
  methods:{
    init(){
      this.isShow = true;
    },
    resetDialog(){
      this.form = JSON.parse(JSON.stringify(this.defaultForm));
      this.new_repassword = null;
    },
    verifyForm(){
      if(!this.$verify.null(this.form.old_password, '请输入旧密码')){
        return false
      }
      let reg = /^(?![A-Za-z]+$)(?![A-Z\d]+$)(?![A-Z\W]+$)(?![a-z\d]+$)(?![a-z\W]+$)(?![\d\W]+$)\S{8,20}$/;
      if (!reg.test(this.form.new_password)) {
        this.$message.warning('新密码长度不得低于8位，由大小写字母、数字组成！');
        return false;
      }
      if(this.form.new_password != this.new_repassword){
        this.$message.warning('两次输入的新密码不一致！');
        return false;
      }

      return true;
    },
    submitForm(){
      if(!this.verifyForm()){
        return;
      }
      this.btnLoading = true;
      this.$request('get', '/user/mod_password'+this.$util.serialize(this.form), {}, (data,msg) => {
        if(data.msg == '操作成功'){
          this.isShow = false;
          this.$message.success('修改成功');
        }else{
          this.$message.error(data.msg);
        }        
        this.btnLoading = false;
        
        // 修改后注销当前token,重新登录
        // this.$store.dispatch("LogOut").then(() => {
        //   location.reload();
        // });
      }, msg => {
        this.btnLoading = false;
      })
    }
  }
}
</script>

<style>

</style>